#!/usr/bin/env bash
#########################################################################
# File Name   : nova.sh.j2
# Author      : seadog
# mail        : seadog0514@163.com
# Version     : 0.1
# Created Time: Mon 2024-09-30 13:17:38(+0800)
#########################################################################

#
# Usage:

{
  set -euo pipefail
}

# source /root/keystonerc

systemctl restart openvswitch-switch
if ! ip a | grep -q 'br-int:'; then
  ovs-vsctl add-br br-int
fi

if [[ ! -e /etc/neutron/plugin.ini ]]; then
  ln -s /etc/neutron/plugins/ml2/ml2_conf.ini /etc/neutron/plugin.ini
fi

su -s /bin/bash neutron -c "neutron-db-manage --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini upgrade head"
systemctl restart ovn-central ovn-northd
sleep 3 # pause is necessary

export controller={{ inventory_hostname }}
ovn-nbctl set-connection ptcp:6641:$controller -- set connection . inactivity_probe=60000
ovn-sbctl set-connection ptcp:6642:$controller -- set connection . inactivity_probe=60000

systemctl restart neutron-server nginx

# !!! flat if
ip link set {{ network_flat_ifname }} up
# add bridge (any name you like for [br-eth1])
ovs-vsctl add-br br-{{ network_flat_ifname }}
# add a port to the bridge
# replace [eth1] to your own environment
ovs-vsctl add-port br-{{ network_flat_ifname }} {{ network_flat_ifname }}
# map [physnet1] to the bridge (any name you like for [physnet1])
ovs-vsctl set open . external-ids:ovn-bridge-mappings=physnet1:br-{{ network_flat_ifname }}

ovs-vsctl set open . external-ids:ovn-remote=tcp:{{ openstack_neutron_ip }}:6642
ovs-vsctl set open . external-ids:ovn-encap-type=geneve,vxlan
ovs-vsctl set open . external-ids:ovn-encap-ip={{ inventory_hostname }}

systemctl restart neutron-server nginx
systemctl restart openvswitch-switch ovn-*

# !!! set gateway_chassis
# Set ovn-cms-options with enable-chassis-as-gw in Open_vSwitch table’s external_ids
# column. Then if this chassis has proper bridge mappings, it will be selected for
# scheduling gateway routers.
ovs-vsctl set open . external-ids:ovn-cms-options=enable-chassis-as-gw

# # !!! setup router 'r1' gateway_chassis
# sleep 3
# ovn-sbctl show
# gw_chassis_id=$(ovn-sbctl show | sed -n '/hostname: {{ openstack_neutron_hostname }}/{g;1!p;};h' | cut -d'"' -f2)
# ovn-nbctl lrp-set-gateway-chassis r1-se1 "${gw_chassis_id}"
# ovn-nbctl list gateway_chassis
# ovn-nbctl show r1

# ovn-nbctl lr-route-add r1 0.0.0.0/0 10.33.1.1
# ovn-nbctl lr-route-list r1
